//
//  TimeClockView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/8.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "TimeClockView.h"

@interface TimeClockView ()
@property (nonatomic, strong) UILabel *hourLabel;
@property (nonatomic, strong) UILabel *minuteLabel;
@property (nonatomic, strong) UILabel *secondLabel;

@property (nonatomic, strong) UILabel *connectHourLabel;
@property (nonatomic, strong) UILabel *connectminuteLabel;

@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic, assign) NSInteger time;
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) UIColor *labelBackgroundColor;
@end

@implementation TimeClockView

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}

- (instancetype)initWithCornerRadius:(CGFloat)cornerRadius {
    self = [super init];
    if (self) {
        _cornerRadius = cornerRadius;
        [self configureUI];
    }
    return self;
}

- (instancetype)initWithCornerRadius:(CGFloat)cornerRadius andColor:(UIColor *)color {
    self = [super init];
    if (self) {
        _cornerRadius = cornerRadius;
        _labelBackgroundColor = color;
        [self configureUI];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.hourLabel.frame = CGRectMake(0, 0, 18, 18);
    self.connectHourLabel.frame = CGRectMake(self.hourLabel.right, 0, 6, 18);
    self.minuteLabel.frame = CGRectMake(self.connectHourLabel.right, 0, 18, 18);
    self.connectminuteLabel.frame = CGRectMake(self.minuteLabel.right, 0, 6, 18);
    self.secondLabel.frame = CGRectMake(self.connectminuteLabel.right, 0, 18, 18);
}

#pragma mark - Method
- (void)configureUI {
    [self addSubview:self.hourLabel];
    [self addSubview:self.minuteLabel];
    [self addSubview:self.secondLabel];
    [self addSubview:self.connectHourLabel];
    [self addSubview:self.connectminuteLabel];
    
    self.hourLabel.layer.cornerRadius = self.cornerRadius;
    self.minuteLabel.layer.cornerRadius = self.cornerRadius;
    self.secondLabel.layer.cornerRadius = self.cornerRadius;
}

- (void)setTimerForReset:(CGFloat)time {
    [self.timer invalidate];
    self.timer = nil;
    // 创建NSTimer对象
    self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    // 加入RunLoop中， scrollview 滑动仍然可以计时
// 1
//    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
//    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode: UITrackingRunLoopMode];
// 2
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

    self.time = time;
}

#pragma mark - Action
- (void)timerAction {
    NSInteger hour = self.time / 3600;
    NSInteger minute = (self.time / 60) % 60;
    NSInteger second = self.time % 60;
    
    NSString *hourStr = @"";
    NSString *minuteStr = @"";
    NSString *secondStr = @"";
    if (hour < 10) {
        hourStr = [NSString stringWithFormat:@"0%ld", (long)hour];
    } else {
        hourStr = [NSString stringWithFormat:@"%ld", (long)hour];
    }

    if (minute < 10) {
        minuteStr = [NSString stringWithFormat:@"0%ld", (long)minute];
    } else {
        minuteStr = [NSString stringWithFormat:@"%ld", (long)minute];
    }
    
    if (second < 10) {
        secondStr = [NSString stringWithFormat:@"0%ld", (long)second];
    } else {
        secondStr = [NSString stringWithFormat:@"%ld", (long)second];
    }
    
    self.hourLabel.text = hourStr;
    self.minuteLabel.text = minuteStr;
    self.secondLabel.text = secondStr;
    
    if (self.time > 0) {
        self.time = self.time - 1;
    } else {
        self.hourLabel.text = @"--";
        self.minuteLabel.text = @"--";
        self.secondLabel.text = @"--";
        [self.timer invalidate];
        if (self.block) {
            self.block();
        }
    }
}

#pragma mark - Getters
- (UILabel *)hourLabel {
    if (!_hourLabel) {
        _hourLabel = [[UILabel alloc] init];
        _hourLabel.font = TextPingFangFontSize(12);
        _hourLabel.clipsToBounds = YES;
        _hourLabel.backgroundColor = self.labelBackgroundColor; // [UIColor colorWithHex:0x4c4c4c alpha:1.0];
        _hourLabel.textColor = [UIColor whiteColor];
        _hourLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _hourLabel;
}

- (UILabel *)minuteLabel {
    if (!_minuteLabel) {
        _minuteLabel = [[UILabel alloc] init];
        _minuteLabel.font = TextPingFangFontSize(12);
        _minuteLabel.clipsToBounds = YES;
        _minuteLabel.backgroundColor = self.labelBackgroundColor;// [UIColor colorWithHex:0x4c4c4c alpha:1.0];
        _minuteLabel.textColor = [UIColor whiteColor];
        _minuteLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _minuteLabel;
}

- (UILabel *)secondLabel {
    if (!_secondLabel) {
        _secondLabel = [[UILabel alloc] init];
        _secondLabel.font = TextPingFangFontSize(12);
        _secondLabel.clipsToBounds = YES;
        _secondLabel.backgroundColor = self.labelBackgroundColor;// [UIColor colorWithHex:0x4c4c4c alpha:1.0];
        _secondLabel.textColor = [UIColor whiteColor];
        _secondLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _secondLabel;
}

- (UILabel *)connectHourLabel {
    if (!_connectHourLabel) {
        _connectHourLabel = [[UILabel alloc] init];
        _connectHourLabel.font = TextPingFangFontSize(12);
        _connectHourLabel.text = @":";
        _connectHourLabel.textColor = _labelBackgroundColor;
        _connectHourLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _connectHourLabel;
}

- (UILabel *)connectminuteLabel {
    if (!_connectminuteLabel) {
        _connectminuteLabel = [[UILabel alloc] init];
        _connectminuteLabel.font = TextPingFangFontSize(12);
        _connectminuteLabel.text = @":";
        _connectminuteLabel.textColor = _labelBackgroundColor;
        _connectminuteLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _connectminuteLabel;
}

- (UIColor *)labelBackgroundColor {
    if (!_labelBackgroundColor) {
        _labelBackgroundColor = [UIColor colorWithHex:0x464553 alpha:1.0];
    }
    return _labelBackgroundColor;
}

@end
